//
//  yiGuanBiViewController.m
//  黑土公社商户端
//
//  Created by 1 on 16/1/25.
//
//

#import "yiGuanBiViewController.h"
#import "HomeViewCell.h"
#import "UIImageView+WebCache.h"
#define URL @"http://app.heitugs.com/app_server/app_order_goods.aspx?";
static NSString *vCellIdentify = @"homeCell";

@interface yiGuanBiViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableview;
}
@end

@implementation yiGuanBiViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.array = [[NSArray alloc] init];
    NSString *urlstr = URL;
    NSURL *myurl = [NSURL URLWithString:urlstr];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl];
    //设置表单提交项
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *str = [ user objectForKey:@"1"];
    NSString *str1 = [request responseString];
    str1 = [str substringWithRange:NSMakeRange(36, 11)];
    //    NSLog(@"截取的值为：%@",str1);
    NSString *str2 = [request responseString];
    str2 = [str substringWithRange:NSMakeRange(62, 2)];
    //    NSLog(@"截取的值为：%@",str2);
    
    [request setPostValue:str1 forKey:@"usernum"];
    [request setPostValue:str2 forKey:@"companyid"];
    [request setPostValue:@"9" forKey:@"order_status"];
    //    [request setDelegate:self];
    __block NSArray *arr = nil;
    [request setCompletionBlock:^{
        //接收字符串数据
        
        _array = [[NSJSONSerialization JSONObjectWithData:request.responseData options:NSUTF8StringEncoding error:nil] retain];
        
        NSLog(@"%@",_array);
        //array 就是那个数组，你需要把这个array的数据  可以在这里也可以在下面那个方法遍历存进那个可变数组里面，
        [_tableview reloadData];
    }];
    [request startAsynchronous];
    
    [self creatTableViewCell];
}
-(void)creatTableViewCell{
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 158) style:UITableViewStylePlain];
    _tableview.delegate= self;
    _tableview.dataSource = self;
    [_tableview registerNib:[UINib nibWithNibName:@"HomeViewCell" bundle:nil] forCellReuseIdentifier:vCellIdentify];
    [self.view addSubview:_tableview];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeViewCell *vCell = [tableView dequeueReusableCellWithIdentifier:vCellIdentify];
    if (vCell == nil) {
        vCell = [[[NSBundle mainBundle] loadNibNamed:@"HomeViewCell" owner:self options:nil] lastObject];
    }
    NSDictionary *dic1 = _array[indexPath.row];
    NSLog(@"dic1= %@",dic1);
    [vCell.ziliaoButton setTitle:[NSString stringWithFormat:@"%@", dic1[@"consignee"]] forState:UIControlStateNormal];
    vCell.daifukuanabel.text = [NSString stringWithFormat:@"%@", dic1[@"pay_status"]];
    vCell.dingdanhaolabel.text = [NSString stringWithFormat:@"%@", dic1[@"order_num"]];
    vCell.shangPinMingButton.text = [NSString stringWithFormat:@"%@", dic1[@"goods_name"]];
    vCell.shijianlabel.text = [NSString stringWithFormat:@"%@", dic1[@"submit_time"]];
    vCell.guiGelabel.text = [NSString stringWithFormat:@"%@", dic1[@"xh"]];
    vCell.shulianglabel.text = [NSString stringWithFormat:@"%@", dic1[@"goods_number"]];
    vCell.hejilabel.text = [NSString stringWithFormat:@"%@", dic1[@"goods_price"]];
    vCell.yunfeilabel.text = [NSString stringWithFormat:@"%@", dic1[@"shipping_fee"]];
    // 拼接字符串
    NSString* string = [@"http://app.heitugs.com" stringByAppendingString:dic1[@"spicurl"]];
    // 转译成中文
    NSString *NSUTF8 = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [vCell.headerImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",NSUTF8]]];

    return vCell;
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}
// 单元格点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%d",indexPath.row);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
